/**
 * Created by Administrator on 2017/3/1 0001.
 */
var ratio=window.devicePixelRatio;
var pixiStageW=1136;
var pixiStageH=740;
var renderer=PIXI.autoDetectRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true,resolution:1});
var stage=new PIXI.Stage(0xFF0000);
var resource;

var div_pixi=document.createElement('div');
document.body.appendChild(div_pixi);
div_pixi.appendChild(renderer.view);

var preload=Preload.create();
preload.init(stage,pixiStageW,pixiStageH);

// var con_whole=new PIXI.Container();
// con_whole.interactive=true;
// stage.addChild(con_whole);

// var emitterContainer;
// emitterContainer = new PIXI.ParticleContainer();
// emitterContainer.setProperties({
//     scale: true,
//     position: true,
//     rotation: true,
//     uvs: true,
//     alpha: true
// });
// stage.addChild(emitterContainer);
//
// var emitter;
// emitter = new PIXI.particles.Emitter(
//     emitterContainer,
//     [new PIXI.Texture.fromImage('assets/images/leaf.png')],
//     {
//         "alpha": {
//             "start": 1,
//             "end": 0
//         },
//         "scale": {
//             "start": 0.6,
//             "end": 0.5,
//             "minimumScaleMultiplier": 1
//         },
//         "color": {
//             "start": "#ffffff",
//             "end": "#ffffff"
//         },
//         "speed": {
//             "start": 30,
//             "end": 20,
//             "minimumSpeedMultiplier": 1
//         },
//         "acceleration": {
//             "x": -300,
//             "y": 311
//         },
//         "maxSpeed": 0,
//         "startRotation": {
//             "min": 0,
//             "max": 360
//         },
//         "noRotation": false,
//         "rotationSpeed": {
//             "min": 0,
//             "max": 10
//         },
//         "lifetime": {
//             "min": 1,
//             "max": 2
//         },
//         "blendMode": "normal",
//         "frequency": 0.001,
//         "emitterLifetime": -1,
//         "maxParticles": 20,
//         "pos": {
//             "x": 300,
//             "y": 300
//         },
//         "addAtBack": false,
//         "spawnType": "rect",
//         "spawnRect": {
//             "x": 0,
//             "y": 0,
//             "w": 100,
//             "h": 800
//         }
//     }
// );
// emitter.emit = true;

pageVer=new PIXI.DisplayObjectContainer();

var sp_verbg =new PIXI.Sprite.fromImage('assets/images/pageInfo_bg.png');
sp_verbg.x=0;
sp_verbg.y=0;
pageVer.addChild(sp_verbg);

var sp_help =new PIXI.Sprite.fromImage('assets/images/verHelp.png');
sp_help.anchor.set(0.5);
sp_help.x=1136/2;
sp_help.y=740/2-120;
pageVer.addChild(sp_help);

new TWEEN.Tween({rotation:0})
    .to({rotation:180*Math.PI/180},2000)
    .onUpdate(function () {
        sp_help.rotation=this.rotation;
    })
    .easing(TWEEN.Easing.Back.InOut)
    .delay(500)
    .yoyo(true)
    .repeat(Infinity)
    .start();

var sp_vert0 =new PIXI.Sprite.fromImage('assets/images/ver_t0.png');
sp_vert0.anchor.set(0.5);
sp_vert0.scale.x=sp_vert0.scale.y=0.8
sp_vert0.x=1136/2;
sp_vert0.y=740/2+120;
pageVer.addChild(sp_vert0);

setTimeout(function () {
    resizeHandler();
},1000)

document.addEventListener('Main_LoadComplete',gameStart);
var arrPage=[];
var con;
var pageVer;
function gameStart(e) {
    resource=e.detail.resources;

    con=new PIXI.DisplayObjectContainer();
    con.interactive=true;
    stage.addChild(con);

    var page0=Page0.create();
    con.addChild(page0.ui);
    page0.ui.x=1136*0;
    page0.In();

    var page1=Page1.create();
    con.addChild(page1.ui);
    page1.ui.x=1136*1;

    var pageInfo=PageInfo.create();
    con.addChild(pageInfo.ui);
    pageInfo.ui.x=1136*2;

    arrPage.push(page0,page1,pageInfo)
    Scroller.create(con,pixiStageW,pixiStageH,'hor',2);
    // var game=Game.create();
    // stage.addChildAt(game.ui,0);
    // var page2=Page2.create();
    // stage.addChild(page2.ui);
    if(window.innerWidth<window.innerHeight)
    {
        if(pageVer)
        {
            stage.addChild(pageVer);
        }

    }
    else
    {
        if(pageVer&&pageVer.parent)
        {
            stage.removeChild(pageVer);
        }
    }
}

var game;
document.addEventListener('Main_GameIn',function (e) {
    con.parent.removeChild(con);
    var game=Game.create();
    stage.addChildAt(game.ui,0);
});
document.addEventListener('Game_ScrollComplete',function (e) {
    console.log('complete!!!!!!!!!!')
    arrPage[e.detail].In();
});
document.addEventListener('Game_ScrollStart',function (e) {
    arrPage[e.detail].Out();
});
document.addEventListener('Main_EndIn',function (e) {
    var page2=Page2.create();
    stage.addChild(page2.ui);

});
document.addEventListener('Main_Page3In',function (e) {
    var page3=Page3.create();
    stage.addChild(page3.ui);
});
function waterWave() {
}


resizeHandler();
window.onresize = resizeHandler;
function resizeHandler() {
    renderer.resize(window.innerWidth, window.innerHeight);
    mode='noBorder';
    switch (mode) {
        case 'exactFit':
            stage.scale.x = window.innerWidth / pixiStageW;
            stage.scale.y = window.innerHeight / pixiStageH;
            break;
        case 'noBorder':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
            stage.scale.y = stage.scale.x;
            break;
        case 'noScale':
            stage.scale.x = 1;
            stage.scale.y = 1;
            break;
        case 'showAll':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
            stage.scale.y = stage.scale.x;
            break;
    }
    stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
    stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;
    if(window.innerWidth<window.innerHeight)
    {
        if(pageVer)
        {
            stage.addChild(pageVer);
        }

    }
    else
    {
        if(pageVer&&pageVer.parent)
        {
            stage.removeChild(pageVer);
        }
    }
};

requestAnimationFrame(animate);
function animate(time) {
    requestAnimationFrame(animate);
    renderer.render(stage);
    TWEEN.update(time);
    // emitter.update(0.01);
}